const {Service}=require("egg")

const {createId}=require("../utils/utils")

class UserService extends Service{

    // 查看
    async showService(id){
        const sql=`select * from user_list where id=?`
        return await this.app.mysql.query(sql,[id])
    }

    // 编辑
    async updateService(id,username,nickname){
        const sql=`update user_list set username=?, nickname=? where id=?`
        return await this.app.mysql.query(sql,[username,nickname,id])
    }

    // 删除;
    async delService(id){
        const sql=`delete from user_list where id=?`
        return await this.app.mysql.query(sql,[id])
    }

    // 增加
    async createServie(username,nickname){
        const id=createId(username)
        const sql=`INSERT INTO user_list(id,username,nickname) VALUES(?,?,?)`
        return await this.app.mysql.query(sql,[id,username,nickname])
    }

    // 获取数据;
    async indexService(){
        const sql=`select * from user_list`
        return await this.app.mysql.query(sql)
    }

    // 判断;
    async changeName(username){
        const sql=`select * from login where username=?`
        return await this.app.mysql.query(sql,[username])
    }

    // 注册
    async registryService(username,pwd){
        const id=createId(username)
        const sql=`INSERT INTO login(id,username,pwd) VALUES(?,?,?)`
        return await this.app.mysql.query(sql,[id,username,pwd])
    }

    // 登录
    async loginService(username,pwd){
        const sql=`select * from login where username=? and pwd=?`
        return await this.app.mysql.query(sql,[username,pwd])
    }
}

module.exports=UserService